/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */


var typeID = $.getUrlVar("id");
if (typeID == undefined)
    typeID = 0;
var typeName = decodeURIComponent($.getUrlVar("name"));
var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");

var info = new Vue({
    el: "#app",
    data: {
        scrollbar: {},
        data: [],
        isLoading: true,
        pageIndex: 1,
        pageSize: PAGESIZE,
        typeName: typeName,

        inputText: '',
        showModal: false,
        shareObj: {
            dataID: '',
            shareTit: '',
            sharePic: ''
        }
    },
    created: function() {
        var dtd = $.Deferred();
        var _self = this;
        //问题列表
        var data = {
            sortType: 1,
            pageSize: _self.pageSize,
            pageIndex: _self.pageIndex,
            typeIDs: typeID
        };
        data = Common.jsonEncode(data);
        _self.scrollbar = new scrollBar({
            isLoading: _self.isLoading,
            url: "ask_getQuestionList",
            type: "get",
            data: data
        });

        _self.loadlist();

    },
    compiled: function() {
        $(document).scroll(function() {
            if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
                info.loadlist(function() {});
            }
        });

        $(document).on('change.inputText', '.fillInput', function() {
            _self.inputText = $(this).val();
        });
    },
    methods: {
        loadlist: function(callback) {

            var _self = this;

            _self.scrollbar.load(function(pageIndex, data) {
                for (var i = 0; i < data.length; i++) {

                    _self.data.push(data[i]);
                }
                _self.pageIndex = pageIndex;
            }, function(isLoading) {
                _self.isLoading = isLoading;
            });

        },
        swipeLeft: function(e) {
            var _self = this;
            if (_self.isEdit === '取消') return; //如果处于编辑状态则不予左滑

            var shareColl = $(e.target).parents('li');
            var touchDrift = ($(e.target).parents('li').width()) / 3;
            var drift = ($(e.target).parents('li').width()) / 2;
            //回复原位置
            _self.isDrift = false; //标识为非左滑状态
            $(e.target).parents('ul').children().animate({ left: 0 });
            $(e.target).parents('ul').children().find('.shareColl').animate({ width: 0, right: 0 });


            console.log(e.deltaX);
            if (e.deltaX < 0) { //左滑
                _self.isDrift = true; //标识为左滑状态
                if (e.deltaX < -touchDrift) {
                    $(e.target).parents('li').animate({ left: -drift });
                    var height = $(e.target).parents('li').outerHeight();
                    shareColl.find('.shareColl').animate({ width: drift, right: -drift }).show().css('line-height', height + 'px');
                }
            } else { //右滑
                if (e.deltaX > touchDrift) {
                    _self.isDrift = false; //标识为非左滑状态
                    $(e.target).parents('li').animate({ left: 0 });
                    shareColl.find('.shareColl').fadeOut();

                }
            }
        },
        share: function(obj, e) {
            $(e.target).parents('li').animate({ left: 0 }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var _self = this;
            _self.showModal = true;
            console.log(obj);
            _self.shareObj = {
                dataID: obj.shareId, //文档id
                shareTit: obj.shareTit, //文档标题
                sharePic: obj.sharePic //文档图片
            };

        },
        shareReg: function(userList) {
            var _self = this;
            if (!userList) { //如果没选择分享对象回到原来界面
                _self.showModal = false;
                return;
            }

            alert({
                close: false,
                text: '分享',
                description: '<div class="clearfix shareTxt"><span class="j_1 photo"><img class="s_1" src="' + _self.shareObj.sharePic + '" onerror=\"javascript:this.src=\'images/icon/viewIcon.png\';this.onerror=null;\"></span><span class="text">' + _self.shareObj.shareTit + '</span></div>',
                fillInput: true,
                kind: 2,
                okEvent: function() {
                    var data = {
                        dataID: _self.shareObj.dataID,
                        userList: userList,
                        comment: _self.inputText,
                        type: 3
                    };
                    data = Common.jsonEncode(data);
                    Common.ajaxCall({ url: 'look_share', datatype: 'json', type: 'get', data: data }, function(data) {
                        if (data.error_code == 0) {
                            var obj = data.data[0];
                            var code = obj.code;
                            var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                            alert({
                                text: '<img src="images/' + pic + '"/>',
                                description: obj.str,
                                kind: 0
                            });
                        } else {
                            alert(data.msg);
                        }
                    });
                }
            });
        },
        //收藏
        Collection: function(obj, e) {
            var _self = this;
            _self.addShow = false;
            $(e.target).parents('li').animate({ left: 0 }).find('.shareColl').fadeOut(); //将左滑的元素复位
            var data = {
                dataID: obj.dataID,
                type: 3,
                isCollection: 1
            };
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'look_addCollection', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    var obj = data.data[0];
                    var code = obj.code;
                    var pic = code == 1 ? "moneyPig.png" : "lebel.png";
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: obj.str,
                        kind: 0
                    });
                } else {
                    alert(data.msg);
                }
            });
        },
        lookAsk: function(id) {
            window.location.href = "askDetail.html?id=" + id + "&back=" + forword;
        },
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        },
        loadPhoto: function() {
            // 比例
            // myJS.imgScale('.j_4', '4', '3');
            // myJS.ImgFull('.loadImg','images/liby.png');
        }

    },
    watch: {
        "data": function() {
            this.loadPhoto();
        }
    }

});